package cn.cnlee.app.ui.home;


import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.adapter.adapter.DelegateAdapter;
import com.adapter.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import cn.cnlee.app.base.BaseListFragment;
import cn.cnlee.app.config.Constants;
import cn.cnlee.app.pojo.AdDTO;
import cn.cnlee.app.pojo.BannerListVo;
import cn.cnlee.app.pojo.CategoryListVo;
import cn.cnlee.app.pojo.ModuleDTO;
import cn.cnlee.app.pojo.ModuleVo;
import cn.cnlee.app.pojo.NewsDTO;
import cn.cnlee.app.pojo.SubscriptionDTO;
import cn.cnlee.app.ui.common.CommonActivity;
import cn.cnlee.app.util.AdapterPool;
import cn.cnlee.app.util.StringUtil;
import cn.cnlee.app.vm.ModuleViewModel;
import cn.cnlee.arch.util.Logger;
import cn.cnlee.arch.view.stateview.LoadingState;

public class ModuleFragment extends BaseListFragment<ModuleViewModel> implements OnItemClickListener {

    public static ModuleFragment newInstance() {
        return new ModuleFragment();
    }

    private ModuleVo moduleVo;

    @Override
    protected void dataObserver() {
        if (getArguments() != null) {
            int pid = getArguments().getInt(Constants.F_PID, 0);
            moduleVo = new ModuleVo(pid);
        }
        registerSubscriber(mViewModel.mRepository.EVENT_KEY_MODULE_DATA, ModuleDTO.class)
                .observe(this, moduleDTO -> {
                    if (moduleDTO != null) {
                        handleData(moduleDTO);
                    }
                });
    }

    private void handleData(ModuleDTO moduleDTO) {
        List<Object> data = new ArrayList<>();
        if (!isLoadMore) {
            if (moduleDTO.ad != null && moduleDTO.ad.size() > 0) {
                data.add(new BannerListVo(moduleDTO.ad));
            }
            if (moduleDTO.subscription != null && moduleDTO.subscription.size() > 0) {
                data.add(new CategoryListVo(moduleDTO.subscription));
            }
        }
        data.addAll(moduleDTO.news == null ? moduleDTO.box : moduleDTO.news);
        setUiData(data);
    }

    @Override
    protected void showLoading() {
        if (!this.isRefresh) {
            loadManager.showStateView(LoadingState.class);
        }else {
            loadManager.showSuccess();
        }
    }

    @Override
    protected void getRepoData() {
        Logger.e("======getRepoData======" + moduleVo.pid);
        mViewModel.getModuleData(moduleVo);
    }

    @Override
    protected void onStateRefresh() {
        super.onStateRefresh();
        getRepoData();
    }

    @Override
    protected DelegateAdapter createAdapter() {
        return AdapterPool.newInstance().getModuleAdapter(getActivity())
                .setOnItemClickListener(this)
                .build();
    }

    @Override
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    }

    @Override
    public void onItemClick(View view, int position, Object object) {
        Logger.e("-----------onItemClick-----------");
        if (object instanceof AdDTO) {
            Logger.e("点击banner");
        } else if (object instanceof SubscriptionDTO) {
            Logger.e("点击分类");
        } else if (object instanceof NewsDTO) {
            Logger.e("点击资讯");
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.F_NEWS_ID, ((NewsDTO)object).id);
            CommonActivity.start(activity, Constants.NEWS_CONTENT_TAG, "文章内容", bundle);
        }
        Logger.e(StringUtil.objToJson(object, true));
    }

    @Override
    public void onRefresh(boolean isRefresh) {
        moduleVo.page = 1;
        super.onRefresh(isRefresh);
    }

    @Override
    public void onLoadMore(boolean isLoadMore, int pageIndex) {
        super.onLoadMore(isLoadMore, pageIndex);
        Logger.e("pageIndex: " + pageIndex);
        moduleVo.page = pageIndex;
        getRepoData();
    }
}
